Kernel 拿头学
安装程序
安装编译过程所需依赖
1
| apt install make gcc bison flex libssl-dev musl-tools
|
下载 linux-4.20 版本的源码(版本看自己需求),推荐用清华提供的下载链接:
https://mirrors.tuna.tsinghua.edu.cn/kernel/v4.x/linux-4.20.tar.gz
编译 linux 内核
1 2 3
| make i386_defconfig make menuconfig make
|
最后一步也可以用 make -j 或者 make -j64,不过吃电脑配置,容易死机
在执行 make menuconfig 的时候,要更改如下选项:
界面一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| *** Compiler: gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609 *** General setup ---> [ ] 64-bit kernel Processor type and features ---> Power management and ACPI options ---> Bus options (PCI etc.) ---> Binary Emulations ---- Firmware Drivers ---> [*] Virtualization ---> General architecture-dependent options ---> [*] Enable loadable module support ---> -*- Enable the block layer ---> IO Schedulers ---> Executable file formats ---> Memory Management options ---> [*] Networking support ---> Device Drivers ---> File systems ---> Security options ---> -*- Cryptographic API ---> Library routines ---> Kernel hacking --->
|
进入 Networking support 的 Networking options
让 <*> The SCTP Protocol
被选中,如左边显示的一样
回到 界面一,进入 Kernel hacking 里面的 Compile-time checks and compiler options
让[*] Compile the kernel with debug info
被选中,如左边显示的一样
然后保存退出后,再使用命令 make -j 即可
安装成功
因为我电脑实在太差了,所以编译成功不容易,在这放个成功的样例:
1 2 3 4
| Setup is 15804 bytes (padded to 15872 bytes). System is 7318 kB CRC 497ced8c Kernel: arch/x86/boot/bzImage is ready (
|
可以看到生成了 bzImage
这里如果碰到如下错误,有两种不同性质解决方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| /tmp/ccPH3fx1.o:在函数‘main’中: extract-cert.c:(.text.startup+0x25):对‘OPENSSL_init_crypto’未定义的引用 extract-cert.c:(.text.startup+0x31):对‘OPENSSL_init_crypto’未定义的引用 collect2: error: ld returned 1 exit status scripts/Makefile.host:90: recipe for target 'scripts/extract-cert' failed make[1]: *** [scripts/extract-cert] Error 1 make[1]: *** 正在等待未完成的任务.... /tmp/ccJdcNg2.o:在函数‘main’中: sign-file.c:(.text.startup+0x53):对‘OPENSSL_init_crypto’未定义的引用 sign-file.c:(.text.startup+0x5f):对‘OPENSSL_init_crypto’未定义的引用 sign-file.c:(.text.startup+0x76b):对‘OPENSSL_init_crypto’未定义的引用 collect2: error: ld returned 1 exit status scripts/Makefile.host:90: recipe for target 'scripts/sign-file' failed make[1]: *** [scripts/sign-file] Error 1 Makefile:1099: recipe for target 'scripts' failed make: *** [scripts] Error 2 make: *** 正在等待未完成的任务....
|
1.没有安装 libssl-dev
安装一下就好 apt-get install libssl-dev
2.链接库不对
查看编译 scripts 目录下的 Makefile,有如下配置:
1 2
| HOSTLDLIBS_sign-file = -lcrypto HOSTLDLIBS_extract-cert = -lcrypto
|
这是编译 sign-file 依赖 crypto 库
终端输入命令 pkg-config --list-all | grep crypto 查看有没有 libcrypto 库
查看如何调用该库,输入命令 pkg-config --libs libcrypto
输出 -L/usr/local/lib -lcrypto
把这句话,复制替换 scripts 目录下的 Makefile:
1 2
| HOSTLDLIBS_sign-file = -L/usr/local/lib -lcrypto HOSTLDLIBS_extract-cert = -L/usr/local/lib -lcrypto
|
重新执行 make -j 命令,成功解决。
注意:如果没有安装 crypto 库,用命令安装 apt-get install libcrypto
编译 poc 所需环境
在 64 位 ubuntu 18.04 下用 gcc -m32 编译 exp 会出错,所以通过 debootstrap 拉取 32 位文件系统来编译 exp
1 2 3 4
| apt install debootstrap libsctp-dev debootstrap --arch=i386 stretch debian_32 http://ftp.cn.debian.org/debian/ debootstrap --arch=amd64 stretch debian_64 http://ftp.cn.debian.org/debian/ chroot ./debian_32
|
再在这个自定义根目录下执行 gcc 的编译命令即可
有拉取失败的情况不慌,可以一直拉取,它会保存你拉取成功的文件,直到你全部拉取成功
编译 poc 要注意的地方
一定要使用静态链接,因为 POC 的利用环境不一定有你程序运行所需要的库
编译 xx 位 poc 的时候或者少库文件的时候,利用上面 chroot ./debian_xx 更改根目录,之后就能畅快使用需要的库文件了